<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin\v1\system\attachment;

use app\controller\admin\Auth;
use app\services\other\CacheServices;
use app\services\system\admin\SystemAdminServices;
use app\services\system\attachment\SystemAttachmentServices;
use think\facade\App;

/**
 * Class：系统附件管理控制器
 * 创建人：Jack
 * 创建时间：2023/8/23 15:12
 * @package app\adminapi\controller\v1\system\attachment
 */
class SystemAttachment extends Auth
{
    /**
     * SystemAttachment constructor.
     * @param App                      $app
     * @param SystemAttachmentServices $services
     */
    public function __construct(App $app, SystemAttachmentServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 注释：附件列表
     * 创建人：Jack
     * 创建时间：2023/8/23 16:21
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['name', ''],
            ['pid', 0],
            ['file_type', 1]
        ]);
        $where['type'] = 1;
        return $this->success($this->services->getImageList($where));
    }

    /**
     * 注释：图片上传
     * 创建人：Jack
     * 创建时间：2023/9/6 11:28
     * @param $upload_type
     * @param $type
     * @return mixed
     */
    public function upload($upload_type = 0, $type = 0)
    {
        [$pid, $file] = $this->request->postMore([
            ['pid', 0],
            ['file', 'file'],
        ], true);
        $res = $this->services->upload((int)$pid, $file, (int)$upload_type, (int)$type);
        return $this->success('上传成功!', ['src' => $res]);
    }

    /**
     * 注释：移动图片
     * 创建人：Jack
     * 创建时间：2023/9/6 11:28
     * @return mixed
     */
    public function moveImageCate()
    {
        $data = $this->request->postMore([
            ['pid', 0],
            ['images', '']
        ]);
        $this->services->move($data);
        return $this->success('移动成功!');
    }

    /**
     * 注释：修改文件名
     * 创建人：Jack
     * 创建时间：2023/9/6 11:01
     * @param $id
     * @return mixed
     */
    public function update($id)
    {
        $realName = $this->request->post('real_name', '');
        if (!$realName) {
            return $this->fail('文件名称不能为空!');
        }
        $this->services->update($id, ['real_name' => $realName]);
        return $this->success('修改成功!');
    }

    /**
     * 注释：删除图片
     * 创建人：Jack
     * 创建时间：2023/8/30 17:24
     * @return mixed
     */
    public function delete()
    {
        [$ids] = $this->request->postMore([
            ['ids', '']
        ], true);
        $this->services->del($ids);
        return $this->success('删除成功!');
    }

    /**
     * 注释：获取上传类型
     * 创建人：Jack
     * 创建时间：2023/9/6 11:29
     * @return mixed
     */
    public function uploadType()
    {
        $data['upload_type'] = (string)sys_config('upload_type', 1);
        return app('json')->success($data);
    }

    /**
     * 注释：视频分片上传
     * 创建人：Jack
     * 创建时间：2023/11/28 17:19
     * @return mixed
     */
    public function videoUpload()
    {
        $data = $this->request->postMore([
            ['chunkNumber', 0], // 第几分片
            ['currentChunkSize', 0], // 分片大小
            ['chunkSize', 0], // 总大小
            ['totalChunks', 0], // 分片总数
            ['file', 'file'], // 文件
            ['md5', ''], // MD5
            ['filename', ''], // 文件名称
            ['pid', 0], // 分类ID
        ]);
        $fileHandle = $this->request->file($data['file']);
        if (!$fileHandle) return $this->fail('上传信息为空!');
        $res = $this->services->videoUpload($data, $fileHandle);
        return app('json')->success($res);
    }

    /**
     * 注释：保存云端视频记录
     * 创建人：Jack
     * 创建时间：2023/11/28 17:20
     * @return mixed
     */
    public function saveVideoAttachment()
    {
        $data = $this->request->postMore([
            ['path', ''], // 视频地址
            ['cover_image', ''], // 封面地址
            ['pid', 0], // 分类ID
            ['upload_type', 1], // 上传类型
        ]);
        $res = $this->services->saveOssVideoAttachment($data, 1, 0, (int)$data['upload_type']);
        return app('json')->success($res);
    }

    /**
     * 注释：获取扫码上传页面链接以及参数
     * 创建人：Jack
     * 创建时间：2023/11/28 17:21
     * @param CacheServices $services
     * @return mixed
     */
    public function scanUploadQrcode(CacheServices $services)
    {
        [$pid] = $this->request->getMore([
            ['pid', 0]
        ], true);
        $uploadToken = md5(time());
        // 文件上传大小kb
        $upload_file_size_max = config('upload.filesize');
        $services->setDbCache('scan_upload', $uploadToken, 68400);
        $url = sys_config('site_url') . '/app/upload?pid=' . $pid . '&cache=scan_upload&type=1&relation_id=0&upload_file_size_max=' . $upload_file_size_max . '&token=' . $uploadToken;
        return app('json')->success(['url' => $url]);
    }

    /**
     * 注释：删除二维码
     * 创建人：Jack
     * 创建时间：2023/11/28 17:22
     * @param CacheServices $services
     * @return mixed
     */
    public function removeUploadQrcode(CacheServices $services)
    {
        $services->deleteDbCache('scan_upload');
        return app('json')->success();
    }

    /**
     * 注释：获取扫码上传的图片数据
     * 创建人：Jack
     * 创建时间：2023/11/28 17:51
     * @param $scan_token
     * @return mixed
     */
    public function scanUploadImage($scan_token)
    {
        return app('json')->success($this->services->scanUploadImage($scan_token));
    }

    /**
     * 注释：网络图片上传
     * 创建人：Jack
     * 创建时间：2023/11/28 17:52
     * @return mixed
     */
    public function onlineUpload()
    {
        $data = $this->request->postMore([
            ['pid', 0],
            ['images', []]
        ]);
        $this->services->onlineUpload($data);
        return app('json')->success('上传完成!');
    }

    /**
     * 注释：获取上传信息
     * 创建人：Jack
     * 创建时间：2023/11/28 17:52
     * @return mixed
     */
    public function getAdminsData()
    {
        /** @var SystemAdminServices $adminServices */
        $adminServices = app()->make(SystemAdminServices::class);
        $data['is_way'] = $adminServices->value(['id' => $this->adminId], 'is_way');
        // 文件上传大小kb
        $data['upload_file_size_max'] = config('upload.filesize');
        return app('json')->success($data);
    }

    /**
     * 注释：保存上传信息
     * 创建人：Jack
     * 创建时间：2023/11/28 17:52
     * @param int $is_way
     * @return mixed
     */
    public function setAdminsData(int $is_way = 0)
    {
        /** @var SystemAdminServices $adminServices */
        $adminServices = app()->make(SystemAdminServices::class);
        if (!in_array($is_way, [0, 1, 2])) return app('json')->fail('参数有误!');
        $res = $adminServices->update($this->adminId, ['is_way' => $is_way]);
        if ($res) {
            return app('json')->success('ok');
        }
        return app('json')->fail('保存失败!');
    }

}